home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Tools / ClassAct / Examples / ListBrowser / ListBrowserExample.c < prev    next >
C/C++ Source or Header  |  2002-03-03  |  15KB  |  541 lines

  1.  
  2. /**
  3.  **  ListBrowserTest.c -- List Browser class test.
  4.  **
  5.  **  This is a simple example testing some of the capabilities of the
  6.  **  ListBrowser gadget class.
  7.  **
  8.  **  This code opens a simple window and then a ListBrowser gadget which is
  9.  **  subsequently attached to the window's gadget list.  Everytime the user
  10.  **  clicks on the close gadget, this code changes some of the attributes
  11.  **  of the ListBrowser gadget to demonstrate different ways it can be used,
  12.  **  including one demonstration which creates two images using the Label
  13.  **  class and shows them in the ListBrowser.
  14.  **
  15.  **/
  16.  
  17.  
  18. #include <exec/types.h>
  19. #include <exec/memory.h>
  20. #include <intuition/intuition.h>
  21. #include <intuition/gadgetclass.h>
  22. #include <intuition/imageclass.h>
  23. #include <intuition/icclass.h>
  24. #include <utility/tagitem.h>
  25. #include <proto/dos.h>
  26. #include <proto/exec.h>
  27. #include <proto/intuition.h>
  28. #include <gadgets/listbrowser.h>
  29. #include <images/label.h>
  30. #include <proto/listbrowser.h>
  31. #include <proto/label.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34.  
  35. /* Soon to be moved to gadgets/listbrowser.h
  36.  */
  37. #define    LBNCA_Editable        (LBNA_Dummy+13)
  38. #define    LBNCA_MaxChars        (LBNA_Dummy+14)
  39. #define    LBNCA_CopyText        (LBNA_Dummy+15)
  40. #define    LISTBROWSER_Editable            (LISTBROWSER_Dummy+33)
  41. #define    LISTBROWSER_Position            (LISTBROWSER_Dummy+34)
  42. #define    LISTBROWSER_EditNode            (LISTBROWSER_Dummy+35)
  43. #define    LISTBROWSER_EditColumn            (LISTBROWSER_Dummy+36)
  44. #define LBP_LINEUP 1
  45. #define LBP_LINEDOWN 2
  46. #define LBP_PAGEUP 3
  47. #define LBP_PAGEDOWN 4
  48. #define LBP_TOP 5
  49. #define LBP_BOTTOM 6
  50.  
  51. #define RAWKEY_CURSORUP 76
  52. #define RAWKEY_CURSORDOWN 77
  53. #define QUALIFIER_SHIFT 0x03
  54. #define QUALIFIER_ALT 0x30
  55. #define QUALIFIER_CTRL 0x08
  56.  
  57.  
  58. /* Function prototypes.
  59.  */
  60. BOOL make_list(struct List *, UBYTE **, LONG *);
  61. VOID free_list(struct List *);
  62. VOID wait_for_close(struct Window *, struct Gadget *);
  63. struct ClassLibrary * OpenClass(STRPTR, ULONG);
  64.  
  65. /* Global variables.
  66.  */
  67. struct ClassLibrary *ListBrowserBase;
  68. struct ClassLibrary *LabelBase;
  69.  
  70.  
  71. UBYTE *col1[] =
  72. {
  73.     "This is a", "test of the", "ListBrowser", "gadget class.",
  74.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  75.     "cool new", "features", "though like", "multiple", "columns,",
  76.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  77.     "and much much", "more!",
  78.     "This is a", "test of the", "ListBrowser", "gadget class.",
  79.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  80.     "cool new", "features", "though like", "multiple", "columns,",
  81.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  82.     "and much much", "more!",
  83.     "This is a", "test of the", "ListBrowser", "gadget class.",
  84.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  85.     "cool new", "features", "though like", "multiple", "columns,",
  86.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  87.     "and much much", "more!", NULL
  88. };
  89.  
  90. LONG col2[] =
  91. {
  92.     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  93.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  94.     27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  95.     39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  96.     51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
  97.     63
  98. };
  99.  
  100. struct ColumnInfo ci[] =
  101. {
  102.     { 80, "Col 1", 0 },
  103.     { 60, "Col 2", 0 },
  104.     { 60, "Col 3", 0 },
  105.     { -1, (STRPTR)~0, -1 }
  106. };
  107.  
  108. struct ColumnInfo fancy_ci[] =
  109. {
  110.     { 100, NULL, 0 },
  111.     { -1, (STRPTR)~0, -1 }
  112. };
  113.  
  114. /* Some fonts for our fancy list.
  115.  */
  116. struct TextAttr helvetica24b = { (STRPTR)"helvetica.font", 24, FSF_BOLD, FPF_DISKFONT };
  117. struct TextAttr times18i = { (STRPTR)"times.font", 18, FSF_ITALIC, FPF_DISKFONT };
  118. struct TextAttr times18 = { (STRPTR)"times.font", 18, 0, FPF_DISKFONT };
  119.  
  120.  
  121. /* This is the start of our programme.
  122.  */
  123. main()
  124. {
  125.     struct Screen *screen = NULL;
  126.  
  127.     /* We'll just open up on the default public screen, and use its screen font.
  128.      */
  129.     if (screen = LockPubScreen(NULL))
  130.     {
  131.         struct Window *win = NULL;
  132.  
  133.         /* Open the window, note how we size the window to perfectly fit
  134.          * all the gadgets.
  135.          */
  136.         if (win = OpenWindowTags(NULL,
  137.             WA_Left, 0,
  138.             WA_Top, screen->Font->ta_YSize + 3,
  139.             WA_Width, 300,
  140.             WA_Height, 160,
  141.             WA_CustomScreen, screen,
  142.             WA_IDCMP, IDCMP_GADGETUP | IDCMP_MOUSEMOVE | IDCMP_RAWKEY |
  143.                         IDCMP_CLOSEWINDOW | IDCMP_GADGETDOWN,
  144.             WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  145.                         WFLG_SIZEGADGET | WFLG_ACTIVATE | WFLG_SMART_REFRESH,
  146.             WA_Title, "ListBrowser Class Demo",
  147.             WA_MinWidth, 50,
  148.             WA_MinHeight, 50,
  149.             WA_MaxWidth, -1,
  150.             WA_MaxHeight, -1,
  151.             TAG_DONE))
  152.         {
  153.             /* Create the private BOOPSI image class.
  154.              */
  155.             PutStr("Creating ListBrowser class\n");
  156.             if (ListBrowserBase = OpenClass("gadgets/listbrowser.gadget", 0))
  157.             {
  158.                 struct Gadget *listbrowser_gad;
  159.                 struct List label_list;
  160.  
  161.                 make_list(&label_list, col1, col2);
  162.  
  163.                 /* Create a listbrowser gadget.
  164.                  */
  165.                 PutStr("Creating ListBrowser object\n");
  166.                 if (listbrowser_gad = (struct Gadget *)NewObject(LISTBROWSER_GetClass(), NULL,
  167.                                                         GA_ID, 1,
  168.                                                         GA_Top, win->BorderTop + 5,
  169.                                                         GA_Left, 10,
  170.                                                         GA_RelWidth, -34,
  171.                                                         GA_RelHeight, -(win->BorderTop + win->BorderBottom + 10),
  172.                                                         GA_RelVerify, TRUE,
  173.                                                         LISTBROWSER_Labels, (ULONG)&label_list,
  174.                                                         LISTBROWSER_ColumnInfo, (ULONG)&ci,
  175.                                                         LISTBROWSER_ColumnTitles, TRUE,
  176.                                                         LISTBROWSER_MultiSelect, FALSE,
  177.                                                         LISTBROWSER_Separators, TRUE,
  178.                                                         LISTBROWSER_ShowSelected, FALSE,
  179.                                                         LISTBROWSER_Editable, TRUE,
  180.                                                         TAG_END))
  181.                 {
  182.                     /* Adding gadgets.
  183.                      */
  184.                     PutStr("Adding gadget\n");
  185.                     AddGList(win, listbrowser_gad, -1, -1, NULL);
  186.                     PutStr("Refreshing gadget\n");
  187.                     RefreshGList(listbrowser_gad, win, NULL, -1);
  188.  
  189.                     /* Wait for close gadget click to continue.
  190.                      */
  191.                     SetWindowTitles(win, "<- Click here to continue", (UBYTE *)~0);
  192.                     wait_for_close(win, listbrowser_gad);
  193.  
  194.                     /* Make Visible
  195.                      */
  196.                     SetWindowTitles(win, "Make Visible 10", (UBYTE *)~0);
  197.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  198.                         LISTBROWSER_MakeVisible, 10,
  199.                         LISTBROWSER_EditNode, 8,
  200.                         LISTBROWSER_EditColumn, 1,
  201.                         TAG_DONE);
  202.                     ActivateGadget(listbrowser_gad, win, NULL);
  203.                     wait_for_close(win, listbrowser_gad);
  204.  
  205.                     /* Show selected
  206.                      */
  207.                     SetWindowTitles(win, "Show selected, Auto-Fit", (UBYTE *)~0);
  208.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  209.                         LISTBROWSER_ShowSelected, TRUE,
  210.                         LISTBROWSER_AutoFit, TRUE,
  211.                         LISTBROWSER_HorizontalProp, TRUE,
  212.                         TAG_DONE);
  213.                     wait_for_close(win, listbrowser_gad);
  214.  
  215.                     /* Multi-select
  216.                      */
  217.                     SetWindowTitles(win, "Multi-select, Virtual Width of 500", (UBYTE *)~0);
  218.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  219.                         LISTBROWSER_MultiSelect, TRUE,
  220.                         LISTBROWSER_VirtualWidth, 500,
  221.                         LISTBROWSER_AutoFit, FALSE,
  222.                         TAG_DONE);
  223.                     wait_for_close(win, listbrowser_gad);
  224.  
  225.                     /* Detach the list.
  226.                      */
  227.                     SetWindowTitles(win, "Detached list", (UBYTE *)~0);
  228.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  229.                         LISTBROWSER_MultiSelect, FALSE,
  230.                         LISTBROWSER_Labels, ~0,
  231.                         TAG_DONE);
  232.                     wait_for_close(win, listbrowser_gad);
  233.  
  234.                     /* No separators, no title, 1 column.
  235.                      */
  236.                     SetWindowTitles(win, "No separators, no title, 1 column.", (UBYTE *)~0);
  237.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  238.                         LISTBROWSER_Labels, (ULONG)&label_list,
  239.                         LISTBROWSER_ColumnInfo, (ULONG)&fancy_ci,
  240.                         LISTBROWSER_Separators, FALSE,
  241.                         LISTBROWSER_ColumnTitles, FALSE,
  242.                         LISTBROWSER_AutoFit, TRUE,
  243.                         LISTBROWSER_VirtualWidth, 0,
  244.                         TAG_DONE);
  245.                     wait_for_close(win, listbrowser_gad);
  246.  
  247.                     /* Fancy list.
  248.                      */
  249.                     PutStr("Creating Label class\n");
  250.                     if (LabelBase = OpenClass("images/label.image", 0))
  251.                     {
  252.                         struct List fancy_list;
  253.                         struct Image *image1, *image2;
  254.                         struct Node *node1, *node2;
  255.  
  256.                         NewList(&fancy_list);
  257.  
  258.                         PutStr("Creating Label object\n");
  259.                         if (image1 = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  260.                                                         IA_FGPen, 1,
  261.                                                         IA_BGPen, 2,
  262.                                                         IA_Font, (ULONG)&helvetica24b,
  263.                                                         LABEL_Text, (ULONG)"C",
  264.                                                         IA_Font, (ULONG)×18i,
  265.                                                         LABEL_Text, (ULONG)"lass ",
  266.                                                         IA_Font, (ULONG)&helvetica24b,
  267.                                                         LABEL_Text, (ULONG)"A",
  268.                                                         IA_Font, (ULONG)×18i,
  269.                                                         LABEL_Text, (ULONG)"ct",
  270.                                                         TAG_END))
  271.                         {
  272.                             PutStr("Creating Label object\n");
  273.                             if (image2 = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  274.                                                             IA_FGPen, 2,
  275.                                                             IA_BGPen, 0,
  276.                                                             IA_Font, (ULONG)×18,
  277.                                                             LABEL_Text, (ULONG)"By Phantom Development",
  278.                                                             TAG_END))
  279.                             {
  280.                                 if (node1 = AllocListBrowserNode(1,
  281.                                                 LBNA_Column, 0,
  282.                                                     LBNCA_Image, (ULONG)image1,
  283.                                                     LBNCA_Justification, LCJ_CENTRE,
  284.                                                 TAG_DONE))
  285.                                 {
  286.                                     AddTail(&fancy_list, node1);
  287.  
  288.                                     if (node2 = AllocListBrowserNode(1,
  289.                                                     LBNA_Column, 0,
  290.                                                         LBNCA_Image, (ULONG)image2,
  291.                                                         LBNCA_Justification, LCJ_CENTRE,
  292.                                                     TAG_DONE))
  293.                                     {
  294.                                         AddTail(&fancy_list, node2);
  295.  
  296.                                         /* Set listbrowser.
  297.                                          */
  298.                                         SetWindowTitles(win, "Fancy", (UBYTE *)~0);
  299.                                         SetGadgetAttrs(listbrowser_gad, win, NULL,
  300.                                             LISTBROWSER_ColumnInfo, &fancy_ci,
  301.                                             LISTBROWSER_Labels, (ULONG)&fancy_list,
  302.                                             LISTBROWSER_AutoFit, TRUE,
  303.                                             TAG_DONE);
  304.  
  305.                                         wait_for_close(win, listbrowser_gad);
  306.  
  307.                                         SetGadgetAttrs(listbrowser_gad, win, NULL,
  308.                                             LISTBROWSER_Labels, ~0,
  309.                                             TAG_DONE);
  310.  
  311.                                         FreeListBrowserNode(node2);
  312.                                     }
  313.                                     FreeListBrowserNode(node1);
  314.                                 }
  315.                                 DisposeObject(image2);
  316.                             }
  317.                             DisposeObject(image1);
  318.                         }
  319.  
  320.                         PutStr("Closing Label class\n");
  321.                         CloseLibrary((struct Library *)LabelBase);
  322.                     }
  323.                     else
  324.                         PutStr("ERROR: Couldn't create Label class\n");
  325.  
  326.                     /* Read-only
  327.                      */
  328.                     SetWindowTitles(win, "Read-only", (UBYTE *)~0);
  329.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  330.                         LISTBROWSER_ColumnInfo, (ULONG)&ci,
  331.                         LISTBROWSER_Labels, (ULONG)&label_list,
  332.                         LISTBROWSER_AutoFit, TRUE,
  333.                         LISTBROWSER_Selected, -1,
  334.                         GA_ReadOnly, TRUE,
  335.                         TAG_DONE);
  336.                     wait_for_close(win, listbrowser_gad);
  337.  
  338.                     /* Disabled.
  339.                      */
  340.                     SetWindowTitles(win, "Disabled", (UBYTE *)~0);
  341.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  342.                         GA_Disabled, TRUE,
  343.                         GA_ReadOnly, FALSE,
  344.                         TAG_DONE);
  345.                     wait_for_close(win, listbrowser_gad);
  346.  
  347.                     /* No scrollbars, borderless.
  348.                      */
  349.                     SetWindowTitles(win, "No scrollbars, borderless", (UBYTE *)~0);
  350.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  351.                         GA_Disabled, FALSE,
  352.                         GA_Top, win->BorderTop,
  353.                         GA_Left, 2,
  354.                         GA_RelWidth, -18,
  355.                         GA_RelHeight, -(win->BorderTop + win->BorderBottom),
  356.                         LISTBROWSER_Borderless, TRUE,
  357.                         LISTBROWSER_HorizontalProp, FALSE,
  358.                         LISTBROWSER_VerticalProp, FALSE,
  359.                         TAG_DONE);
  360.                     wait_for_close(win, listbrowser_gad);
  361.  
  362.                     RemoveGList(win, listbrowser_gad, -1);
  363.                     DisposeObject(listbrowser_gad);
  364.                 }
  365.                 else
  366.                     PutStr("ERROR: Couldn't create ListBrowser gadgetn");
  367.  
  368.                 free_list(&label_list);
  369.  
  370.                 PutStr("Freeing ListBrowser class\n");
  371.                 CloseLibrary((struct Library *)ListBrowserBase);
  372.             }
  373.             else
  374.                 PutStr("ERROR: Couldn't create ListBrowser class\n");
  375.  
  376.             CloseWindow(win);
  377.         }
  378.         else
  379.             PutStr("ERROR: Couldn't open window\n");
  380.  
  381.         UnlockPubScreen(0, screen);
  382.     }
  383.     else
  384.         PutStr("ERROR: Couldn't lock public screen\n");
  385. }
  386.  
  387.  
  388. /* Function to make a List of ListBrowserNodes from a couple of arrays.
  389.  * Just to demonstrate things, we make make three columns, 2 with text
  390.  * (the same text) and the third with numbers.
  391.  */
  392. BOOL make_list(struct List *list, UBYTE **labels1, LONG *labels2)
  393. {
  394.     struct Node *node;
  395.     WORD i = 0;
  396.  
  397.     NewList(list);
  398.  
  399.     while (*labels1)
  400.     {
  401.         if (node = AllocListBrowserNode(3,
  402.                         LBNA_Column, 0,
  403.                             LBNCA_CopyText, TRUE,
  404.                             LBNCA_Text, *labels1,
  405.                             LBNCA_MaxChars, 40,
  406.                             LBNCA_Editable, TRUE,
  407.                         LBNA_Column, 1,
  408.                             LBNCA_CopyText, TRUE,
  409.                             LBNCA_Text, *labels1,
  410.                             LBNCA_MaxChars, 40,
  411.                             LBNCA_Editable, TRUE,
  412.                         LBNA_Column, 2,
  413.                             LBNCA_Integer, &labels2[i],
  414.                             LBNCA_Justification, LCJ_RIGHT,
  415.                         TAG_DONE))
  416.         {
  417.             AddTail(list, node);
  418.         }
  419.         else
  420.             break;
  421.  
  422.         labels1++;
  423.         i++;
  424.     }
  425.     return(TRUE);
  426. }
  427.  
  428.  
  429. /* Function to free an Exec List of ListBrowser nodes.
  430.  */
  431. VOID free_list(struct List *list)
  432. {
  433.     struct Node *node, *nextnode;
  434.  
  435.     node = list->lh_Head;
  436.     while (nextnode = node->ln_Succ)
  437.     {
  438.         FreeListBrowserNode(node);
  439.         node = nextnode;
  440.     }
  441.     NewList(list);
  442. }
  443.  
  444.  
  445. /* Function to open a BOOPSI class library.
  446.  */
  447. struct ClassLibrary * OpenClass(STRPTR name, ULONG version)
  448. {
  449.     struct Library *retval;
  450.     UBYTE buffer[256];
  451.  
  452.     if ((retval = OpenLibrary(name, version)) == NULL)
  453.     {
  454.         sprintf (buffer, ":classes/%s", name);
  455.         if ((retval = OpenLibrary(buffer, version)) == NULL)
  456.         {
  457.             sprintf(buffer, "classes/%s", name);
  458.             retval = OpenLibrary(buffer, version);
  459.         }
  460.     }
  461.     return((struct ClassLibrary *)retval);
  462. }
  463.  
  464. /* Wait for the window close gadget to be pressed.
  465.  */
  466. VOID wait_for_close(struct Window *win, struct Gadget *listbrowser_gad)
  467. {
  468.     BOOL ok = TRUE;
  469.  
  470.     /* Just wait around until the close gadget is pressed.
  471.      */
  472.     while (ok)
  473.     {
  474.         struct Gadget *gadget;
  475.         struct IntuiMessage *imsg;
  476.             
  477.         WaitPort(win->UserPort);
  478.         while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  479.         {
  480.             switch (imsg->Class)
  481.             {
  482.                 case IDCMP_CLOSEWINDOW:
  483.                     ok = FALSE;
  484.                     break;
  485.  
  486.                 case IDCMP_RAWKEY:
  487.                     if (!(imsg->Code & IECODE_UP_PREFIX))
  488.                     {
  489.                         switch (imsg->Code)
  490.                         {
  491.                             case RAWKEY_CURSORUP:
  492.                                 if (imsg->Qualifier & QUALIFIER_CTRL)
  493.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  494.                                         LISTBROWSER_Position, LBP_TOP,
  495.                                         TAG_DONE);
  496.                                 if (imsg->Qualifier & QUALIFIER_SHIFT)
  497.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  498.                                         LISTBROWSER_Position, LBP_PAGEUP,
  499.                                         TAG_DONE);
  500.                                 else
  501.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  502.                                         LISTBROWSER_Position, LBP_LINEUP,
  503.                                         TAG_DONE);
  504.                                 break;
  505.  
  506.                             case RAWKEY_CURSORDOWN:
  507.                                 if (imsg->Qualifier & QUALIFIER_CTRL)
  508.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  509.                                         LISTBROWSER_Position, LBP_BOTTOM,
  510.                                         TAG_DONE);
  511.                                 if (imsg->Qualifier & QUALIFIER_SHIFT)
  512.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  513.                                         LISTBROWSER_Position, LBP_PAGEDOWN,
  514.                                         TAG_DONE);
  515.                                 else
  516.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  517.                                         LISTBROWSER_Position, LBP_LINEDOWN,
  518.                                         TAG_DONE);
  519.                                 break;
  520.  
  521.                             default:
  522.                                 break;
  523.                         }
  524.                     }
  525.                     break;
  526.  
  527.                 case IDCMP_GADGETUP:
  528.                     gadget = (struct Gadget *)imsg->IAddress;
  529.                     Printf("Gadget: %ld  Code: %ld\n",
  530.                         (LONG)gadget->GadgetID, (LONG)imsg->Code);
  531.  
  532.                     break;
  533.         
  534.                 default:
  535.                     break;
  536.             }
  537.             ReplyMsg((struct Message *)imsg);
  538.         }
  539.     }
  540. }
  541.